#!/bin/bash
clear

TEXT='\033[30;107;2m'
INFO='\033[97;44m'
WARNING='\033[97;43m'
ERROR='\033[97;41m'
SUCCESS='\033[97;42m'
GREEN='\033[92m'
YELLOW='\033[93m'
RESET='\033[0m'

if [ "${EUID}" -ne 0 ]; then
    echo -e "${ERROR} ERROR ${RESET}${TEXT} You need to run this script as root! ${RESET}"; exit 1
fi

cek=$(cat /home/limit)
NIC=$(ip -o $ANU -4 route show to default | awk '{print $5}');

function start {
    echo -e "Limit Speed All Service"
    read -p "Set maximum download rate (in Kbps): " down
    read -p "Set maximum upload rate (in Kbps): " up
    if [[ -z "$down" ]] && [[ -z "$up" ]]; then
        echo > /dev/null 2>&1
    else
        wondershaper -a $NIC -d $down -u $up > /dev/null 2>&1
        systemctl enable --now wondershaper.service
        echo "start" > /home/limit
        echo "Done"
    fi
}

function stop {
    wondershaper -ca $NIC
    systemctl stop wondershaper.service
    echo "Stop Configuration"
    sleep 0.5
    echo > /home/limit
    echo "Done"
}

if [[ "$cek" = "start" ]]; then
    sts="[ON]"
else
    sts="[OFF]"
fi

clear
echo -e " =============================="
echo -e "     Limit Bandwidth Speed         "
echo -e " =============================="
echo -e " Status $sts"
echo -e "  1. Start Limit"
echo -e "  2. Stop Limit"
echo -e " Press CTRL+C to return"
read -rp " Please Enter The Correct Number : " -e choose
if [[ "$choose" = "1" ]]; then
    start
elif [[ "$choose" = "2" ]]; then
    stop
else
    clear; echo " You Entered The Wrong Number" && menu
fi
